package com.qire.manhua.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.ogaclejapan.smarttablayout.SmartTabLayout;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * 可拦截的
 */
public class CanInterceptViewPager extends ViewPager {

    private boolean interceptState = false;
    private SmartTabLayout smartTabLayout;

    public CanInterceptViewPager(Context context) {
        super(context);
    }

    public CanInterceptViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return !this.interceptState && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return !this.interceptState && super.onInterceptTouchEvent(event);
    }

    /**
     * 启用或停用拦截滑动
     * @param interceptState true 启用 false 禁用，默认 false 不拦截左右滑动
     */
    public void setInterceptState(boolean interceptState) {
        this.interceptState = interceptState;
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
        super.setAdapter(adapter);
        if(smartTabLayout != null) {
            smartTabLayout.setViewPager(this);
        }
    }

    public void bindSmartTabLayout(SmartTabLayout smartTabLayout){
        this.smartTabLayout = smartTabLayout;
        smartTabLayout.setViewPager(this);
    }
}
